package shuzu

import (
	"fmt"
	"math"
	"sort"
)

func SortedSquares(nums []int) []int {
	for i := 0; i < len(nums); i++ {
		nums[i] *= nums[i]
	}
	sort.Slice(nums, func(i, j int) bool {
		return nums[i] < nums[j]
	})
	return nums
}

// 自己完成一个快排

func quickSort(nums []int) []int {
	return nums
}

func SortedSquares2(nums []int) []int {
	sort.Slice(nums, func(i, j int) bool {
		return math.Abs(float64(nums[i])) < math.Abs(float64(nums[j]))
	})
	for i := 0; i < len(nums); i++ {
		nums[i] *= nums[i]
	}
	return nums
}

func SortedSquares3(nums []int) []int {
	return nums
}

func SortedSquares4(nums []int) []int {
	// 首先，原数组就是从小到大排序，然后进行比较首位的平方，取其大的值，最后移动对应指针
	n := len(nums) - 1
	a, b := 0, n
	ans := make([]int, n+1)
	for n >= 0 {
		if nums[a]*nums[a] > nums[b]*nums[b] {
			ans[n] = nums[a] * nums[a]
			a++
		} else {
			ans[n] = nums[b] * nums[b]
			b--
		}
		n--
		fmt.Println(ans)
	}
	return ans
}

func leetcode(nums []int) []int {
	n := len(nums)
	ans := make([]int, n)
	i, j := 0, n-1
	for pos := n - 1; pos >= 0; pos-- {
		if v, w := nums[i]*nums[i], nums[j]*nums[j]; v > w {
			ans[pos] = v
			i++
		} else {
			ans[pos] = w
			j--
		}
	}
	return ans
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}
